package com.heima.admin.gateway.filter;

import com.alibaba.fastjson.JSON;
import com.heima.admin.gateway.utils.AppJwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedTransferQueue;

@Component
@Slf4j
@Order(0)//此注解的意思表示若有多个过滤器，过滤器的执行的优先级。越小优先级越高
public class AuthorizeFilter implements GlobalFilter {
    private static List<String> allowUrls=new ArrayList<>();//放行的白名单的路径
    static {
        allowUrls.add("/login/in");//放行登录方法
        allowUrls.add("/v2/api-dos");
    }
    /**
     *
     * @param exchange //封装了请求和响应的对象
     * @param chain//表示链对象
     * @return
     */


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.通过exchange获取请求对象
        ServerHttpRequest request = exchange.getRequest();
        //2.检查请求路径是否需要拦截
        String path = request.getURI().getPath();//得到请求路径
        for (String allowUrl : allowUrls) {
            if (path.contains(allowUrl)) {//说明不需要拦截
               return chain.filter(exchange);//放行
            }
        }
        //3. 获取请求头中token信息
        String token = request.getHeaders().getFirst("token");
        //如果token不存在，则直接终止请求返回401
        if (StringUtils.isBlank(token)) {
        //终止请求
           return writeMessage(exchange,"请登录后再访问！");
        }
        //解析token
        try {
            Claims claimsBody = AppJwtUtil.getClaimsBody(token);
            int i = AppJwtUtil.verifyToken(claimsBody);//-1：有效，0：有效，1：过期，2：过期
            if(i<1){
                Integer id = claimsBody.get("id",Integer.class);//获取登录用户的id
                //解析成功
                //获取token中存储的用户的id 将用户id写入到请求头中，路由给其他微服务
                request.mutate().header("userId",String.valueOf(id));//修改request
                exchange.mutate().request(request);
                log.info("解析token成功 当前登录用户id==>{}",id);
                return chain.filter(exchange);
            }
            log.error("token已过期");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("解析token出现异常",e.getMessage());
        }


        return writeMessage(exchange,"解析用户token失败，请重新登录");
    }
    private Mono<Void> writeMessage(ServerWebExchange exchange, String msg) {
        Map map =new HashMap<>();
        map.put("code","401");
        map.put("errorMessage",msg);
        //获取响应对象
        ServerHttpResponse response = exchange.getResponse();
        //设置响应的状态码
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        //设置响应的数据格式
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        //设置响应的数据
        DataBuffer wrap = response.bufferFactory().wrap(JSON.toJSONBytes(map));
        //响应数据
        return response.writeWith(Flux.just(wrap));


    }
}
